package com.dhcc.ws.common.config;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dhcc.ws.common.interceptor.BaseDataInterceptor;
import com.dhcc.ws.common.interceptor.HelloAuthInterceptor;
import com.dhcc.ws.common.interceptor.WebServiceInterceptor;
import com.dhcc.ws.webservice.service.basedata.BaseDataService;
import com.dhcc.ws.webservice.service.demo.DemoService;
import com.dhcc.ws.webservice.service.weather.WeatherService;

@Configuration
public class CxfConfig {

	@Bean
	public ServletRegistrationBean cxfServletRegistration() {
		ServletRegistrationBean registrationBean = new ServletRegistrationBean(new CXFServlet());
		registrationBean.setName("cxfServletRegistration");
		registrationBean.setOrder(600);
		registrationBean.addUrlMappings("/ws/*");
		return registrationBean;
	}
	@Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    
	@Bean("weatherService")
	public Endpoint weatherService(SpringBus springBus) {
		EndpointImpl endpoint = new EndpointImpl(springBus, new WeatherService());
		endpoint.getInInterceptors().add(new HelloAuthInterceptor());
		endpoint.publish("/weatherService");
		return endpoint;
	}
	@Bean("demoService")
	public Endpoint demoService(SpringBus springBus) {
		EndpointImpl endpoint = new EndpointImpl(springBus, new DemoService());
		endpoint.getInInterceptors().add(new WebServiceInterceptor());
		endpoint.publish("/demoService");
		return endpoint;
	}
	@Bean("baseDataService")
	public Endpoint baseDataService(SpringBus springBus) {
		EndpointImpl endpoint = new EndpointImpl(springBus, new BaseDataService());
		endpoint.getInInterceptors().add(new BaseDataInterceptor());
		endpoint.publish("/baseDataService");
		return endpoint;
	}
	
}
